package com.sdy.po;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Order {

    private Integer id;         //id
    private Integer companyId;  //客户公司的id
    private Date time;          //生成时间
    private Date payTime;       //付款时间
    private String UUID;        //uuid
    private List<OrderItem> orderItemList;
    private String salesPersion;  //销售员
    private BigDecimal totalPrice; //总价
    private Boolean state;       //状态

    public Order(Integer id, Integer companyId, Date time, Date payTime, String UUID, List<OrderItem> orderItemList, String salesPersion, BigDecimal totalPrice, Boolean state) {
        this.id = id;
        this.companyId = companyId;
        this.time = time;
        this.payTime = payTime;
        this.UUID = UUID;
        this.orderItemList = orderItemList;
        this.salesPersion = salesPersion;
        this.totalPrice = totalPrice;
        this.state = state;
    }

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", companyId=" + companyId +
                ", time=" + time +
                ", payTime=" + payTime +
                ", UUID='" + UUID + '\'' +
                ", orderItemList=" + orderItemList +
                ", salesPersion='" + salesPersion + '\'' +
                ", totalPrice=" + totalPrice +
                ", state=" + state +
                '}';
    }

    public Boolean getState() {
        return state;
    }

    public void setState(Boolean state) {
        this.state = state;
    }

    public Order(Integer id, Integer companyId, Date time, Date payTime, String UUID, List<OrderItem> orderItemList, String salesPersion, BigDecimal totalPrice) {
        this.id = id;
        this.companyId = companyId;
        this.time = time;
        this.payTime = payTime;
        this.UUID = UUID;
        this.orderItemList = orderItemList;
        this.salesPersion = salesPersion;
        this.totalPrice = totalPrice;
    }

    public BigDecimal getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(BigDecimal totalPrice) {
        this.totalPrice = totalPrice;
    }

    public Order(Integer id, Integer companyId, Date time, Date payTime, String UUID, List<OrderItem> orderItemList, String salesPersion) {
        this.id = id;
        this.companyId = companyId;
        this.time = time;
        this.payTime = payTime;
        this.UUID = UUID;
        this.orderItemList = orderItemList;
        this.salesPersion = salesPersion;
    }

    public String getSalesPersion() {
        return salesPersion;
    }

    public void setSalesPersion(String salesPersion) {
        this.salesPersion = salesPersion;
    }

    public Order(Integer companyId, String UUID) {
        this.companyId = companyId;
        this.UUID = UUID;
    }

    public Order() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Integer companyId) {
        this.companyId = companyId;
    }

    public String getTime(String format) {
        SimpleDateFormat temp=new SimpleDateFormat(format);
        return temp.format(time);
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public Date getTime(){
        return time;
    }

    public Date getPayTime(){
        return payTime;
    }

    public String getPayTime(String format) {
        SimpleDateFormat temp=new SimpleDateFormat(format);
        return temp.format(payTime);
    }

    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    public String getUUID() {
        return UUID;
    }

    public void setUUID(String UUID) {
        this.UUID = UUID;
    }

    public List<OrderItem> getOrderItemList() {
        return orderItemList;
    }

    public void setOrderItemList(List<OrderItem> orderItemList) {
        this.orderItemList = orderItemList;
    }

    public void specificOrder() {
        for (OrderItem orderItem : this.getOrderItemList()) {
            Product product = orderItem.getProduct();
            for (Specification specification : product.getSpecificationList()) {
                if (specification.getId() == orderItem.getSpecificationId()) {
                    product.setSpecification(specification);
                    if (orderItem.getPrice() == null){
                        BigDecimal price = specification.getPrice().multiply(new BigDecimal(orderItem.getNumber()*(100-orderItem.getDiscount())).divide(new BigDecimal(100))).setScale(2, BigDecimal.ROUND_HALF_UP);
                        orderItem.setPrice(price);
//                        this.totalPrice = this.totalPrice.add(price);
                    }
                    break;
                }
            }
            product.setSpecificationList(null);
        }
    }
}
